import {customRef} from "vue";

// 自定义Ref
export default function (initValue : string, delay : number) {
    let timer : number;
    // track跟踪, trigger触发
    const msg = customRef((track, trigger) => {
        return {
            // msg被调用时触发
            get() {
                track();
                return initValue;
            },
            // msg被修改时触发
            set(value) {
                clearTimeout(timer);
                timer = setTimeout(() => {
                    initValue = value;
                    trigger();
                }, delay)
            }
        }
    })
    return {msg};
}